stages:
  - build
  - upload
  - deploy


before_script:
  - docker info
#  - docker-compose --version

build_database:
  stage: build
  rules:
    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
  script:
    - docker build -t database:$CI_COMMIT_SHORT_SHA ./database

build_backend:
  stage: build
  rules:
    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
  script:
    - docker build -t backend:$CI_COMMIT_SHORT_SHA ./backend

#build_frontend:
#  stage: build
#  rules:
#    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
#  script:
#    - docker build -t frontend:$CI_COMMIT_SHORT_SHA ./frontend

upload_database:
  stage: upload
  rules:
    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
  needs:
    - job: build_database
  script:
    - kind load docker-image database:${CI_COMMIT_SHORT_SHA}


upload_backend:
  stage: upload
  rules:
    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
  needs:
    - job: build_backend
  script:
    - kind load docker-image backend:${CI_COMMIT_SHORT_SHA}


#upload_frontend:
#  stage: upload
#  rules:
#    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
#  needs:
#    - job: build_frontend
#  script:
#    - kind load docker-image frontend:${CI_COMMIT_SHORT_SHA}

deploy:
  stage: deploy
  rules:
    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
  needs:
    - job: upload_database
    - job: upload_backend
#    - job: upload_frontend
  environment:
    name: production

  script:
#    - kubectl get pods
    - kubectl set image deployment/django-deployment django-app=backend:${CI_COMMIT_SHORT_SHA}
    - kubectl get pods -o wide
